unit U_Localizar;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, ExtCtrls, DB, ComCtrls, StdCtrls,
  Buttons, Grids, DBGrids, DBClient,  IBCustomDataSet, ShellApi,IBQuery;





type
  TCustomDBGridCracker = class(TCustomDBGrid);
  TAfterSearchEvent = procedure (Sender: TObject; AModalResult: integer) of object;
  TTypeError = (teError, teWarning);

  TF_Localizar = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    DBGrid1: TDBGrid;
    btnOk: TBitBtn;
    btnCancel: TBitBtn;
    StatusBar1: TStatusBar;
    DataSource1: TDataSource;
    procedure btnOkClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
  private
    FAfterSearch: TAfterSearchEvent;
    procedure ConfigGrid;
    procedure DoAfterSearch(AModalResult: integer);




  public
    pesqDataSet:TIBQuery;
    ClausulaWhere:AnsiString;
    pesqDescrColumn, pesqDescrColumn1, pesqCampoTabela, pesqNomeTabela, pesqString, pesqOrderby,pr1,pr2,pr3,pr4,pr5:String;

    procedure Buscar;
  end;
var
  F_Localizar: TF_Localizar;
implementation

uses uFunRede, U_menu, mcFuncs;



{$R *.dfm}


{ TfrLocalizarEx }
procedure TF_Localizar.btnOkClick(Sender: TObject);
begin
  DoAfterSearch(mrOk);
  Close;
  ModalResult := mrOk;
end;

procedure TF_Localizar.btnCancelClick(Sender: TObject);
begin
  DoAfterSearch(mrCancel);
  Close;
  ModalResult := mrCancel;
end;

procedure TF_Localizar.Edit1KeyPress(Sender: TObject; var Key: Char);

begin
{: se apertou ENTER, realiza a busca}
  if key = #13 then
  begin
    if Assigned(pesqDataSet) then
    begin
      Buscar;
      StatusBar1.SimpleText := Format('%d registro(s) encontrado(s) com "%s"',
        [pesqDataSet.RecordCount, Edit1.Text]);
      if pesqDataSet.RecordCount = 0 then
         Mensagem('Nenhum Registro encontrado'+#13+'com "'+copy(Edit1.Text,1,20)+'"');  
      Key:=#0;
    end;
    if pesqDataSet.IsEmpty then
      Edit1.SetFocus
    else
      DBGrid1.SetFocus;
  end;
end;

procedure TF_Localizar.Buscar;
begin
  // ShowMessage(ClausulaWhere);
  if trim(ClausulaWhere)<>''then
  begin
    if trim(Edit1.Text)<> ''then
    begin
     // ShowMessage(ClausulaWhere);
      pesqDataSet.Close;
      pesqDataSet.SQL.Clear;
      pesqDataSet.Sql.Add(ClausulaWhere);
      pesqDataSet.ParamByName('vBusca').AsString := trim('%'+trim(Edit1.Text)+'%');
      if trim(pr1)<>''then  pesqDataSet.ParamByName('p1').AsString := trim(pr1);
      if trim(pr2)<>''then  pesqDataSet.ParamByName('p2').AsString := trim(pr2);
      if trim(pr3)<>''then  pesqDataSet.ParamByName('p3').AsString := trim(pr3);
      if trim(pr4)<>''then  pesqDataSet.ParamByName('p4').AsString := trim(pr4);
      if trim(pr5)<>''then  pesqDataSet.ParamByName('p5').AsString := trim(pr5);
      pesqDataSet.Sql.Add(' order by '+pesqOrderby);
      pesqDataSet.Open;
      ConfigGrid;
    end;
  end else
  begin
    if trim(Edit1.Text)<> ''then
    begin
      pesqDataSet.Close;
      pesqDataSet.SQL.Clear;
      pesqDataSet.SQL.add('Select *from '+pesqNomeTabela);
      pesqDataSet.Sql.Add('WHERE '+pesqCampoTabela+'  like  :vBusca');
      pesqDataSet.ParamByName('vBusca').AsString := trim('%'+trim(Edit1.Text)+'%');
      pesqDataSet.Sql.Add(' order by '+pesqOrderby);
      pesqDataSet.Open;
      ConfigGrid;
    end;
  end;
end;

procedure TF_Localizar.DBGrid1DblClick(Sender: TObject);
begin
  if Assigned(pesqDataSet) then
    if pesqDataSet.RecordCount > 0 then
      begin
        DoAfterSearch(mrOk);
        Close;
        ModalResult := mrOk;
      end;
end;

procedure TF_Localizar.DataSource1DataChange(Sender: TObject;
  Field: TField);
begin
  if Assigned(pesqDataSet) then
    btnOk.Enabled := pesqDataSet.RecordCount > 0;
end;

procedure TF_Localizar.ConfigGrid;
var
  i : integer;
begin
{: configura as duas colunas do Grid}
  if Assigned(pesqDataSet) then
  begin
    DBGrid1.Columns[0].Title.Caption := pesqDescrColumn;
    DBGrid1.Columns[1].Title.Caption := pesqDescrColumn1;
    for i := 0 to 1 do
      begin
        DBGrid1.Columns[i].Title.Font.Name := 'Verdana';
        DBGrid1.Columns[i].Title.Font.Style := [fsBold];
      end;
    DBGrid1.Columns[0].Title.Alignment := taCenter;
    DBGrid1.Columns[0].Alignment := taCenter;
    DBGrid1.Columns[0].Width := 80;
    DBGrid1.Columns[1].Width := 340;
 //   DBGrid1.Color:= F_Principal.CorDoGride;

  end;
end;

procedure TF_Localizar.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
{: se apertar ESC fecha o form}
if Assigned(pesqDataSet) then
begin
  if Key = VK_Escape then
    begin
      pesqDataSet.Close;
      Close;
      DoAfterSearch(mrCancel);
    end;
  {: se apertar F1 limpa a busca}
  if Key = VK_F1 then
    begin
      Edit1.Text := '';
      Edit1.SetFocus;
      pesqDataSet.Close;
    end;
end;
end;

procedure TF_Localizar.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    if DataSource1.DataSet.RecordCount > 0 then
    begin
      DoAfterSearch(mrOk);
      Close;
      ModalResult := mrOk;
    end
    else
    begin
      DoAfterSearch(mrCancel);
      ModalResult := mrCancel;
    end;
end;

procedure TF_Localizar.FormShow(Sender: TObject);
begin
  DBGrid1.Color:=Menu_Retaguarda.vCorBarraDoGrid;
  DataSource1.DataSet:=pesqDataSet;
  Edit1.Text:=pesqString;
  pesqDataSet.Close;
  pesqDataSet.SQL.Clear;
  if not Assigned(pesqDataSet) then
    raise EPropriedadeNaoSetada.Create('DataSet');
  if pesqDataSet.RecordCount > 0 then
  StatusBar1.SimpleText := Format('%d registro(s) encontrado(s)', [pesqDataSet.RecordCount, Edit1.Text]);
  If trim(Edit1.Text)<> '' Then
   begin
     Edit1.SetFocus;
     keybd_event( VK_RETURN, 0, 0, 0 );
   end;
end;

procedure TF_Localizar.DoAfterSearch(AModalResult: integer);
begin
  if Assigned(FAfterSearch) then
    FAfterSearch(Self, AModalResult);
end;

procedure TF_Localizar.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  with TCustomDBGridCracker(Sender) do
  begin
   if DataLink.ActiveRecord = Row - 1 then
   begin
     Canvas.Brush.Color := clInfoBk;
     Canvas.Font.Style := [fsBold];
     Canvas.Font.Color := clBlack; //clFuchsia;//
     Canvas.Font.Name := 'Verdana';
     Canvas.Font.Size := 8;
   end
   else
     Canvas.Font.Color := clBlack;
   DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
end;

procedure TF_Localizar.FormCreate(Sender: TObject);
begin
  // SetaCores(F_Marcas,Menu_Retaguarda.vCorBarraBotoes,Menu_Retaguarda.vCorBarraDoGrid);
  Edit1.Color:=Menu_Retaguarda.vCorBarraDoGrid;
end;

end.
